/***************************************************************************
 创建者：       hualei
 开始时间：    2018.09.10
 copyright:(C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "movefreeinstructiontrackview.h"
#include "ui_movefreeinstructiontrackview.h"
#include "../../programView/programview.h"
#include "movefreeinstructionview.h"

MoveFreeInstructionTrackView::MoveFreeInstructionTrackView(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::MoveFreeInstructionTrackView)
{
    ui->setupUi(this);

    saveFlag = false;

    setGeometry(100,100,288,376);
    this->setStyleSheet("background: papayawhip;");
    fileString << "";

    ui->TrackFileName_Edit->setFocusPolicy(Qt::NoFocus);
    //*******文件列表****************************
    dirModel = new QFileSystemModel;
    //中文标头
    dirModel->setHeaderData(0,Qt::Horizontal,"文件名");
    dirModel->setHeaderData(1,Qt::Horizontal,"大小");
    //设置排序方式
//    index = dirModel->setRootPath(D_TRACK_FILE_PATH);    //路径:存在情况如果文件夹本身打不了怎么办,程序就启动不起了

    QStringList filter;
    filter << "*.ini" << "*.txt" << "*.xml";        //过滤器
    dirModel->setNameFilters(filter);

    //false为隐藏
    dirModel->setNameFilterDisables(false);

    ui->TrackFile_Tree_Widget->setAnimated(false);
    ui->TrackFile_Tree_Widget->setSortingEnabled(true);
//    ui->TrackFile_Tree_Widget->setModel(model);          //树界面设置
    ui->TrackFile_Tree_Widget->setRootIndex(index);
    ui->TrackFile_Tree_Widget->expand(index);               //当前项展开
    ui->TrackFile_Tree_Widget->scrollTo(index);             //定位到当前项
    ui->TrackFile_Tree_Widget->resizeColumnToContents(0);

    ui->TrackFile_Tree_Widget->header()->hide();
    ui->TrackFile_Tree_Widget->setColumnHidden(2,true);
    ui->TrackFile_Tree_Widget->setColumnHidden(3,true);
    ui->TrackFile_Tree_Widget->setColumnWidth(0,300);

    mMoveing=false;
    //Qt::FramelessWindowHint 无边框
    //Qt::WindowStaysOnTopHint 窗口在最顶端，不会拖到任务栏下面
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint |Qt::WindowStaysOnTopHint);
    this->setStyleSheet("QDialog{border:2px solid green;}");

}


void MoveFreeInstructionTrackView::changeLanguage()
{
    ui->retranslateUi(this);
}

void MoveFreeInstructionTrackView::initView()
{
    trackFilePath = D_TRACK_FILE_PATH;
}

void MoveFreeInstructionTrackView::getInput()
{
    trackFilePath = ui->TrackFileName_Edit->text();
}

void MoveFreeInstructionTrackView::updateView()
{
    ui->TrackFileName_Edit->setText(trackFilePath);
}

void MoveFreeInstructionTrackView::on_TrackFile_Cancel_Btn_clicked()
{
    this->close();
}

void MoveFreeInstructionTrackView::on_TrackFile_Sure_Btn_clicked()
{   
    saveFlag = true;
    getInput();
    emit trackFileSeletionView_Sure_Signal(trackFilePath, modeIndex);
    this->close();
}

void MoveFreeInstructionTrackView::on_TrackFile_Tree_Widget_clicked(const QModelIndex &index)
{
    trackFilePath = dirModel->fileInfo(index).fileName();   //fileName(index);
    modeIndex = index;
}

void MoveFreeInstructionTrackView::mousePressEvent(QMouseEvent *event)
{
    mMoveing = true;
    //记录下鼠标相对于窗口的位置
    //event->globalPos()鼠标按下时，鼠标相对于整个屏幕位置
    //pos() this->pos()鼠标按下时，窗口相对于整个屏幕位置
    mMovePosition = event->globalPos() - pos();
    return QDialog::mousePressEvent(event);
}

void MoveFreeInstructionTrackView::mouseMoveEvent(QMouseEvent *event)
{
    //(event->buttons() && Qt::LeftButton)按下是左键
    //鼠标移动事件需要移动窗口，窗口移动到哪里呢？就是要获取鼠标移动中，窗口在整个屏幕的坐标，然后move到这个坐标，怎么获取坐标？
    //通过事件event->globalPos()知道鼠标坐标，鼠标坐标减去鼠标相对于窗口位置，就是窗口在整个屏幕的坐标
    if (mMoveing && (event->buttons() && Qt::LeftButton)
        && (event->globalPos()-mMovePosition).manhattanLength() > QApplication::startDragDistance())
    {
        move(event->globalPos()-mMovePosition);
        mMovePosition = event->globalPos() - pos();
    }
    return QDialog::mouseMoveEvent(event);
}

void MoveFreeInstructionTrackView::mouseReleaseEvent(QMouseEvent *event)
{
    mMoveing = false;
}
